var app=getApp();
var ux = require('../../../../lib/ux.js');
var QRCode = require('../../../../utils/weapp-qrcode.js')
var qrcode;

const W = wx.getSystemInfoSync().windowWidth;
const rate = 750.0 / W;
// 300rpx 在6s上为 150px
const code_w = 460 / rate; 
Page({

  /**
   * 页面的初始数据
   */
  data: {  
    rmsg:"暂无物流信息",
    thisMobile:"",
    thisImg:"",
    image: '',
    code_w: code_w,
    qxUrl:"/api/orders/cancelOrder.do",
    shUrl:"/api/orders/getGoodsOk.do",
    tkUrl:"/api/orders/returnMoney.do",   
    tsUrl:"/api/orders/tSOrder.do",
    dELUrl:"/api/orders/delOrder.do",
    kdUrl:"/api/memberUser/queryKD.do",
    shouhuo:false,
    isShow:true,
    hexiao:false,
    tousus:'',
    tuikuanliyouo:'',  
    tousu:false,
    tuikuan:false,
    orderQ:false,
    orderWL:false,
    orderD:false,
    tel:'15888888888',
    lianxi:false,
    scroll:true,
    tab:0,
    list:[[],[],[],[],[],[],[]],
    pages:[0,0,0,0,0,0,0],
    nexts:[true,true,true,true,true,true,true],
    size:10,
    winheight: 0,
    winwidth: 0,
    action:"/api/orders/memberOrders.do",
	  submit:"{{submit}}"
  },
  preventTouchMoveTy:function(){
    return;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    var self=this;
    this.setData({
      merchant: wx.getStorageSync("banrdId")
    })
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth;
        self.setData({
          winheight: clientHeight,
          winwidth: clientWidth
        });
      }
    });
    if (options.tab){
      this.setData({
          tab: options.tab
    })
    }else{
     this.setData({
        tab: 0
      })
    }    
    qrcode = new QRCode('canvas', {
      // usingIn: this,
      text: "kong",
      image: '/lib/dute.mp/res/bbv.png',
      width: code_w,
      height: code_w,
      colorDark: "#000000",
      colorLight: "white",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },
  loadMore:function(){
    this.getList();
  },
  getList: function () {
    var self = this;
    
    if (self.data.nexts[this.data.tab] == false) {
      wx.showToast({
        title: '没有更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var params = { banrdId:self.data.merchant, type: this.data.tab, pageNo: parseInt(self.data.pages[this.data.tab]), pageSize: self.data.size };
    ux.request(self.data.action, params, function (data) {
      var datas = data.data.list;
      if (data.data.pageInfo.next == true) {
        var nexts=self.data.nexts;
        nexts[self.data.tab]=true;
        self.setData({
          nexts: nexts
        });
      } else {
        var nexts = self.data.nexts;
        nexts[self.data.tab] = false;
        self.setData({
          nexts: nexts
        });
      }
      if (self.data.pages[self.data.tab] == 0) {
        var lists=self.data.list;
        lists[self.data.tab]=datas;
        self.setData({
          list: lists
        });
      } else {
        var lists = self.data.list;
        lists[self.data.tab] = lists[self.data.tab].concat(datas);
        self.setData({
          list: lists
        });
      }
      var pages = self.data.pages;
      pages[self.data.tab] = pages[self.data.tab]+1;
      self.setData({
        pages: pages
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    },true);
  },
  onForm:function(event){
	if(this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
		  ux.request(this.data.submit,detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo(dataset.url);
					}
				}
		  },true);
	 }
  },
  
  onTap:function(event){
	  var dataset = event.currentTarget.dataset;
	  var data = this.data;
	  if(dataset.key && dataset.val+""!=""){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },
  onBar: function (event) {
    var dataset = event.currentTarget.dataset;
    var data = this.data;
    if (dataset.key && dataset.val + "" != "") {
      if (dataset.val!=this.data.tab){
        if (this.data.list[dataset.val].length>0){
          data[dataset.key] = dataset.val;
           }else{
          data[dataset.key] = dataset.val;
               this.getList();
           }
       }else{
        data[dataset.key] = dataset.val;
       }
      
    }
    this.setData(data);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if(this.data.isShow){
      this.setData({
        shouhuo:false,
        hexiao: false,
        tousus: '',
        tuikuanliyouo: '',
        tousu: false,
        tuikuan: false,
        orderQ: false,
        tel: '15888888888',
        lianxi: false,
        scroll: true,
        list: [[], [], [], [], [], [],[]],
        pages: [0, 0, 0, 0, 0, 0,0],
        nexts: [true, true, true, true,true, true, true],
        isShow:false
      })
      this.getList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  
  cancelTel:function(){
    this.setData({
      lianxi:false
    })
  },
  tel: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.thisMobile,
    })
  },
  openTel: function (event){
    var dataset = event.currentTarget.dataset;
    var thisMobile = this.data.list[this.data.tab][dataset.index].phone;
    this.setData({
      thisMobile: thisMobile,
      lianxi: true
    })
  },
  gotoDetile: function (event){
    var dataset = event.currentTarget.dataset;
    var obj = this.data.list[this.data.tab][dataset.index];
    wx.navigateTo({
      url: '/pages/member/tab3/order/detail?idx='+dataset.index+'&id=' + obj.id,
    })
  },
  orderDOpen: function (event){
    var dataset = event.currentTarget.dataset;
    var obj = this.data.list[this.data.tab][dataset.index];
    this.setData({
      thisItemID: obj.id,
      thisIndexs: dataset.index,
      orderD: true
    })
  },
  orderQOpen: function (event) {
    var dataset = event.currentTarget.dataset;
    var obj = this.data.list[this.data.tab][dataset.index];
    this.setData({
      thisItemID: obj.id,
      thisIndexs: dataset.index,
      orderQ: true
    })
  },
  orderWLOpen:function(event){
    var self=this;
    var dataset = event.currentTarget.dataset;
    var obj = this.data.list[this.data.tab][dataset.index];
    if (obj.sType==2){
      wx.showLoading({
        title: '加载中...',
      })
      var parms = { orderId: obj.id };
      ux.request(this.data.kdUrl, parms, function (protocol) {
        wx.hideLoading();
        console.log(JSON.stringify(protocol.data))
        if(protocol.data.type){
          var result = protocol.data.result;
          // result.data.sort((a, b) => {
          //   return a.time > b.time ? 1 : -1;
          // })
          wx.navigateTo({
            url: '/pages/member/tab3/order/wl?msg='+JSON.stringify(result),
          })
        }else{
          self.setData({
            thisItemID: obj.id,
            thisIndexs: dataset.index,
            orderWL: true,
            rmsg: "暂无物流消息！"
          })
        }
      }, true);

    } else if (obj.sType == 1){
      this.setData({
        thisItemID: obj.id,
        thisIndexs: dataset.index,
        orderWL: true,
        rmsg:"该订单由附近签约配送商配送中！"
      })
    }
    
    
   
  },
 
  orderWLcancel: function () {
    this.setData({
      orderWL: false
    })
  },
  orderQcancel: function () {
    this.setData({
      orderQ: false
    })
  },
  orderDcancel: function () {
    this.setData({
      orderD: false
    })
  },
  tuikuanOk: function (e) {
    var self = this;
    if (!this.data.tuikuanliyouo) {
      self.toast("请输入退款理由");
      return false;
    }
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    
    var parmas = {formId:e.detail.formId, id: this.data.thisItemID, liyou: self.data.tuikuanliyouo };
    ux.request(this.data.tkUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          tuikuanliyouo: "",
          tuikuan: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          tuikuanliyouo: "",
          tuikuan: false
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  tousuOk: function () {
    var self = this;
    if (!this.data.tousus){
      self.toast("请输入投诉理由");
     return false;
    }
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var parmas = { id: this.data.thisItemID, liyou: self.data.tousus};
    ux.request(this.data.tsUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          tousus:"",
          tousu: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          tousus: "",
          tousu: false
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  dEL: function () {
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var self = this;
    var parmas = { id: this.data.thisItemID };
    ux.request(this.data.dELUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          orderD: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          orderD: false
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  QX:function(){
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    var self = this;
    var parmas = { id: this.data.thisItemID };
    ux.request(this.data.qxUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type == 1) {
        self.setData({
          orderQ: false,
          isShow: true
        })
        self.toast(data.data.msg);
        self.onShow();
      } else {
        self.setData({
          orderQ: false
        })
        self.toast(data.data.msg);
      }
    }, true);
  },
  gotoPayPage: function (event){
    var dataset = event.currentTarget.dataset;
    var obj=this.data.list[this.data.tab][dataset.index];
    var urls='';
    if (obj.orderType==1){//直接购买订单
      this.setData({
        isShow:true
      })
      urls ='/pages/member/tab2/confirmZhiOrder?orderId='+obj.id;
    }else{
      this.setData({
        isShow: true
      })
      urls = '/pages/member/tab2/confirmOrder?orderId=' + obj.id;
    }
    wx.navigateTo({
      url: urls,
    })
  },
  opentousu: function (event) {
    var dataset = event.currentTarget.dataset;
    var obj = this.data.list[this.data.tab][dataset.index];
    this.setData({
      thisItemID: obj.id,
      thisIndexs: dataset.index,
      tousu: true
    })
  },
  xtousu:function(){
    this.setData({
      tousus:"",
      tousu: false
    })
  },
  opentuikuan: function (event) {
    var dataset = event.currentTarget.dataset;
    var obj = this.data.list[this.data.tab][dataset.index];
    this.setData({
      thisItemID: obj.id,
      thisIndexs: dataset.index,
      tuikuan: true
    })
  },
  xtuikuan: function(){
    this.setData({
      tuikuanliyouo:"",
      tuikuan: false
    })
  },
  tuikuanliyouo:function(e){
    this.setData({
      tuikuanliyouo:e.detail.value
    })
  },
  tousus:function(e){
    
    this.setData({
      tousus: e.detail.value
    })
  },
  openxhexiao:function(event){
    console.log(JSON.stringify(event.detail.fromId));
    var dataset = event.currentTarget.dataset;
    var obj = this.data.list[this.data.tab][dataset.index];
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      thisItemID:obj.id,
      thisImg: obj.no,
      hexiao: true
    })
    qrcode.makeCode(this.data.thisImg)
    wx.hideLoading();
  },
  xhexiao:function(){
    this.setData({
      hexiao: false
    })
  },
  hexiaoOk: function () {//核销完毕刷新页面
    this.setData({
      hexiao:false,
      isShow:true
    })
    this.onShow();
  },
  shouhuoOpen: function (event) {
    var dataset = event.currentTarget.dataset;
    var obj = this.data.list[this.data.tab][dataset.index];
    this.setData({
      thisItemID: obj.id,
      thisIndexs:dataset.index,
      shouhuo: true
    })
  },
  shouhuoOk:function(){//掉后台确认收货
   wx.showLoading({
     title: '处理中...',
     mask:true
   })
   var self=this;
    var parmas = { id: this.data.thisItemID};
    ux.request(this.data.shUrl, parmas, function (data) {
      wx.hideLoading();
      if (data.data.type==1) {
          self.setData({
            shouhuo:false,
            isShow:true
            })
        self.toast(data.data.msg);
        self.onShow();
        }else{
        self.setData({
          shouhuo: false
        })
          self.toast(data.data.msg);
        }
    }, true);
  },
  shouhuocancel:function(){
    this.setData({
      shouhuo: false
    })
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  }

})
